#include "lh_graph.h"

void Graph_Delay(void)
{
   u16 i = 1;
    while(--i);
}


void LH_GraphEve(void *widget)
{
    LH_WIDGET *DilgFoc = widget;
 
    if (DilgFoc->WidFlags & LH_WIDREDR) 
    {
        LH_S16 *Ppdata  = (LH_S16 *)DilgFoc->PData;
        LH_S16 OrigX    = *Ppdata;
        LH_S16 OrigY    = *(Ppdata + 1);
        LH_S16 UnitSize = *(Ppdata + 2);
        LH_S16 *Data1 = Ppdata + 4;
        LH_S16 *Data2 = Ppdata + 4 +   UnitSize;
        LH_S16 *Data3 = Ppdata + 4 + 2*UnitSize;
        LH_U16 i1;
        
        CPU_IntDis();
        if(*(Ppdata + 3) == 0)  
        {
            
            for(i1 = 0; i1 < UnitSize; i1 ++)
                *(Data2 + i1) = *(Data1 + i1);



            
            for(i1 = 0; i1 < UnitSize; i1 ++)
            {
                Graph_Delay();
                TFT_SetPoint(OrigX + i1, OrigY + *(Data2 + i1), DilgFoc->WidColor);
            }
            
            *(Ppdata + 3) = 1;  
        }
        else
        {
            
            for(i1 = 0; i1 < UnitSize; i1 ++)
            {
                Graph_Delay();
                TFT_SetPoint(OrigX + i1, OrigY + *(Data2 + i1), *(Data3 + i1));
            }
            
            for(i1 = 0; i1 < UnitSize; i1 ++)
                *(Data2 + i1) = *(Data1 + i1);
            


            
            for(i1 = 0; i1 < UnitSize; i1 ++)
            {
                Graph_Delay();
                TFT_SetPoint(OrigX + i1, OrigY + *(Data2 + i1), DilgFoc->WidColor);
            }
        }
        CPU_IntEn();
    }
}



void GraphPdataInit(LH_WIDGET *graph)
{
    LH_S16 *Ppdata  = (LH_S16 *)graph->PData;
    LH_S16 UnitSize = *(Ppdata + 2);
    LH_S16 *Data1   = Ppdata + 4;
    LH_S16 i1;
    
    for(i1 = 0; i1 < 3*UnitSize; i1 ++)
        *(Data1 + i1) = 0;
}



void GraphSetOrigXY(LH_WIDGET *graph, LH_U16 origx, LH_U16 origy)
{
    LH_S16 *Ppdata = (LH_S16 *)graph->PData;
    
    *Ppdata = origx;
    *(Ppdata + 1) = origy;
}



void GraphSetUnitSize(LH_WIDGET *graph, LH_U16 unitsize)
{
    LH_S16 *Ppdata = (LH_S16 *)graph->PData;

    *(Ppdata + 2) = unitsize;
}


void SetGraphFirstDra(LH_WIDGET *graph)
{
    LH_S16 *Ppdata = (LH_S16 *)graph->PData;
    
    *(Ppdata + 3) = 0;
}


void GraphAddData(LH_WIDGET *graph, LH_U16 data)
{
    static LH_U16 PosiP = 0;
    LH_S16 *Ppdata = (LH_S16 *)graph->PData;
    LH_S16 UnitSize = *(Ppdata + 2);
    LH_S16 *Data1 = Ppdata + 4;
    LH_U16 i1;
    
    if(PosiP == UnitSize)
    {
        for(i1 = 0; i1 < UnitSize-1; i1 ++)
            *(Data1 + i1) = *(Data1 + i1 + 1);
        *(Data1 + i1) = data;
    }
    else
        *(Data1 + PosiP) = data;
    
    if(PosiP < UnitSize)
        PosiP ++;
}








